
/**
 * created on 2012-03-21
 * @author  qiujz
 * @version 1.0
 *
 * Copyright (c)  Founder Software (Suzhou) Co., Ltd.  2012
 *  All rights reserved.
 *
 * 
 * ================================================================
 * Change Revision
 * ---------------------------------------------------------------
 *    DATE                BY                   COMMENT
 * ---------------------------------------------------------------
 * 2012-03-21         qiujz                Create class
 * 
 *
 * ================================================================
 */

package com.founder.bm.service.impl;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.jws.WebService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.founder.mh.framework.entity.QueryResultBase;
import com.founder.mh.framework.util.BuildTableUtil;
import com.founder.bm.dao.IEnrollDtlDao;
import com.founder.bm.dao.IEnrollInfDao;
import com.founder.bm.dao.ILoginUserDao;
import com.founder.bm.entity.EnrollDtlTo;
import com.founder.bm.entity.EnrollInfTo;
import com.founder.bm.entity.LoginUserQueryParameter;
import com.founder.bm.entity.LoginUserTo;
import com.founder.bm.service.ILoginUserService;
import com.founder.bm.util.ExcelUtil;
import com.founder.mh.framework.entity.Sort;
import com.founder.mh.framework.service.impl.BaseServiceImpl;

@WebService(endpointInterface = "com.founder.bm.service.ILoginUserService")
public class LoginUserServiceImpl extends BaseServiceImpl implements ILoginUserService {

	private static final Log LOGGER = LogFactory.getLog(LoginUserServiceImpl.class);

	private ILoginUserDao loginUserDao;
	
	private IEnrollInfDao enrollInfDao;
	
	public IEnrollInfDao getEnrollInfDao() {
		return enrollInfDao;
	}

	public void setEnrollInfDao(IEnrollInfDao enrollInfDao) {
		this.enrollInfDao = enrollInfDao;
	}

	public IEnrollDtlDao getEnrollDtlDao() {
		return enrollDtlDao;
	}

	public void setEnrollDtlDao(IEnrollDtlDao enrollDtlDao) {
		this.enrollDtlDao = enrollDtlDao;
	}

	private IEnrollDtlDao enrollDtlDao;
	
	/**
	 * @return Dao接口
	 */
	public ILoginUserDao getLoginUserDao() {
		return loginUserDao;
	}
	
	/**
	 * @param loginUserDao
	 *            Dao接口
	 */
	public void setLoginUserDao(ILoginUserDao loginUserDao) {
		this.loginUserDao = loginUserDao;
	}
	
	/** 
	 * @inheritDoc
	 * @see com.founder.bm.service.ILoginUserService
	 *      #searchLoginUserList(com.founder.bm.entity.LoginUserQueryParameter)
	 */
	public QueryResultBase searchLoginUserList(LoginUserQueryParameter paramter) {
		if (paramter == null) {
			throw new RuntimeException("查询参数对象不能为空!");
		}
		
		// 放置查询结果的对象
		QueryResultBase rtnResult = new QueryResultBase();

		// 设置查询参数
		Map<String, Object> paramMap = new HashMap<String, Object>();
		// ToDO Add parameter
		paramMap.put(BuildTableUtil
							.buildPropertyNameFromColumn(LoginUserTo.USER_ID), paramter.getLoginUser().getUserId());
		paramMap.put(BuildTableUtil
				.buildPropertyNameFromColumn(LoginUserTo.PASSWORD), paramter.getLoginUser().getPassword());
		paramMap.put(BuildTableUtil
				.buildPropertyNameFromColumn(LoginUserTo.IS_ADMIN), paramter.getLoginUser().getIsAdmin());

		List<LoginUserTo> loginUserList = null;
		int total = loginUserDao.getLoginUserCount(paramMap);
		rtnResult.setTotal(total);
		if (total == 0) {
			return rtnResult;
		}
		
		// 设置排序字段
		List<Sort> sortList = paramter.getSort();
		if (sortList != null && sortList.size() > 0) {
			StringBuilder sb = new StringBuilder();
			for (Sort sort : sortList) {
				sb.append(sort.getColumnName()).append(" ").append(
						sort.getOrder()).append(",");
			}

			paramMap.put("sort", sb.toString()
					.substring(0, sb.lastIndexOf(",")));

			if (LOGGER.isDebugEnabled()) {
				LOGGER.debug(paramMap.get("sort"));
			}
		}
		
		// 不分页的情况
		if (paramter.getStartRow() <= 0) {
			loginUserList= loginUserDao.searchLoginUserList(paramMap);
		} else {
			// 执行分页询
			int endRow = paramter.getStartRow() + paramter.getLimit() - 1;
			loginUserList = loginUserDao.searchLoginUserPagination(paramMap,
					paramter.getStartRow(), endRow);
		}
		for (LoginUserTo loginUserTo : loginUserList) {
			HashMap<String, Object> hm = new HashMap<String, Object>();
			hm.put("userId", loginUserTo.getUserId());
			EnrollInfTo enrollInfo = enrollInfDao.getEnrollInfByPrimaryKey(loginUserTo.getUserId());
			List<EnrollDtlTo> enrollDtlList = enrollDtlDao.searchEnrollDtlList(hm);
			loginUserTo.setEnrollInfo(enrollInfo);
			loginUserTo.setEnrollDtlList(enrollDtlList);
		}
		rtnResult.setPageData(loginUserList);
		return rtnResult;
	}
	
	public String exportData(LoginUserQueryParameter paramter) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put(BuildTableUtil
				.buildPropertyNameFromColumn(LoginUserTo.USER_ID), paramter.getLoginUser().getUserId());
		paramMap.put(BuildTableUtil
				.buildPropertyNameFromColumn(LoginUserTo.IS_ADMIN), paramter.getLoginUser().getIsAdmin());
		List<LoginUserTo> dataList = loginUserDao.searchLoginUserList(paramMap);
		for (LoginUserTo loginUserTo : dataList) {
			HashMap<String, Object> hm = new HashMap<String, Object>();
			hm.put("userId", loginUserTo.getUserId());
			EnrollInfTo enrollInfo = enrollInfDao.getEnrollInfByPrimaryKey(loginUserTo.getUserId());
			List<EnrollDtlTo> enrollDtlList = enrollDtlDao.searchEnrollDtlList(hm);
			loginUserTo.setEnrollInfo(enrollInfo);
			loginUserTo.setEnrollDtlList(enrollDtlList);
		}
		
		return ExcelUtil.saveGeneratedExcel(ExcelUtil.baoMingDataExcel(dataList), 12L, "报名信息导出");
	}
	
	public String exportDataTXt(LoginUserQueryParameter paramter) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put(BuildTableUtil
				.buildPropertyNameFromColumn(LoginUserTo.USER_ID), paramter.getLoginUser().getUserId());
		paramMap.put(BuildTableUtil
				.buildPropertyNameFromColumn(LoginUserTo.IS_ADMIN), paramter.getLoginUser().getIsAdmin());
		List<LoginUserTo> dataList = loginUserDao.searchLoginUserList(paramMap);
		for (LoginUserTo loginUserTo : dataList) {
			HashMap<String, Object> hm = new HashMap<String, Object>();
			hm.put("userId", loginUserTo.getUserId());
			EnrollInfTo enrollInfo = enrollInfDao.getEnrollInfByPrimaryKey(loginUserTo.getUserId());
			List<EnrollDtlTo> enrollDtlList = enrollDtlDao.searchEnrollDtlList(hm);
			loginUserTo.setEnrollInfo(enrollInfo);
			loginUserTo.setEnrollDtlList(enrollDtlList);
		}
		
		return ExcelUtil.saveGeneratedTXT(dataList, 12L, "报名信息导出");
	}
	
	
	/** 
	 * @inheritDoc
	 * @see com.founder.bm.service.ILoginUserService
	 *      #getLoginUserByPrimaryKey(String)
	 */
	public LoginUserTo getLoginUserByPrimaryKey(String userId) {
		return loginUserDao.getLoginUserByPrimaryKey(userId);
	}
	
	
	/** 
	 * @inheritDoc
	 * @see com.founder.bm.service.ILoginUserService
	 *      #insertLoginUser(com.founder.bm.entityLoginUser.LoginUserTo)
	 */
	public LoginUserTo insertLoginUser(LoginUserTo loginUser) {
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("userId", loginUser.getUserId());
		List<LoginUserTo> list = loginUserDao.searchLoginUserList(map);
		if(list.size() > 0){ //修改
			loginUserDao.updateLoginUserByBean(loginUser);
		}else{
			loginUserDao.insertLoginUser(loginUser);
		}
//			 loginUser = loginUserDao.getLoginUserByPrimaryKey(userId);
		return loginUser;
	}
	
	/** 
	 * @inheritDoc
	 * @see com.founder.bm.service.ILoginUserService
	 *      #updateLoginUser(com.founder.bm.entity.LoginUserTo)
	 */
	public int updateLoginUser(LoginUserTo loginUser) {
		return loginUserDao.updateLoginUserByBean(loginUser);		
	}
	
	
	/** 
	 * @inheritDoc
	 * @see com.founder.bm.service.ILoginUserService
	 *      #deleteLoginUser(String)
	 */
	public int deleteLoginUser(String userId) {
		return loginUserDao.deleteLoginUserByPrimaryKey(userId);
	}
	
	/** 
	 * @inheritDoc
	 * @see com.founder.bm.service.ILoginUserService
	 *      #deleteLoginUserBatch(String[])
	 */
	public void deleteLoginUserBatch(String[] keyArray) {
		loginUserDao.deleteLoginUserBatch(keyArray);
	}
	
}
	
